package vendalenger.debvi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;

public class Preferences {

	public static int i_comment;
	public static int i_digit;
	public static int i_non;
	public static int i_none;
	public static int i_state;
	public static int i_string;
	public static ArrayList<String> knownFileFormats;
	public static ArrayList<String> knownFileFormatsCommand;
	public static String lastLocation;
	public static String r_comment;
	public static String r_digit;
	public static String r_non;
	public static String r_none;
	public static String r_state;
	public static String r_string;
	public static boolean textWrap = false;
	public static String theme = "Arlioove";

	public static void setup() {
		if (new File("debvi.pref").exists()) {
			System.out.println("Loading preferences...");
			File preferences = new File("debvi.pref");
			try {
				FileInputStream fis = new FileInputStream(preferences);
				Properties prop = new Properties();
				prop.load(fis);
				knownFileFormats = new ArrayList<String>();
				knownFileFormatsCommand = new ArrayList<String>();
				lastLocation = prop.getProperty("lastLocation");
				theme = prop.getProperty("lookAndFeel");
				r_none = prop.getProperty("tec_none");
				r_digit = prop.getProperty("tec_digits");
				r_non = prop.getProperty("tec_nons");
				r_string = prop.getProperty("tec_strings");
				r_state = prop.getProperty("tec_states");
				r_comment = prop.getProperty("tec_comments");
				String[] kff = prop.getProperty("kff").split("\\$");
				String[] kffc = prop.getProperty("kffc").split("\\$");
				for (String s : kff) {
					knownFileFormats.add(s);
				}
				for (String s : kffc) {
					knownFileFormatsCommand.add(s);
				}
				fis.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("debvi.pref is nonexistent!");
			theme = "Nimbus";
			r_none = "000000";
			r_digit = "114f00";
			r_non = "9e9600";
			r_string = "d90502";
			r_state = "#0c00e8";
			r_comment = "f000cc";
			knownFileFormats = new ArrayList<String>();
			knownFileFormatsCommand = new ArrayList<String>();
			knownFileFormats.add("js");
			knownFileFormats.add("txt");
			knownFileFormats.add("fcis");

			knownFileFormats.add("png");
			knownFileFormats.add("gif");

			knownFileFormats.add("jpg");
			knownFileFormats.add("jpeg");
			knownFileFormats.add("jpe");
			knownFileFormats.add("jif");
			knownFileFormats.add("jfif");
			knownFileFormats.add("jfi");

			knownFileFormats.add("dent");
			knownFileFormats.add("dspt");

			knownFileFormatsCommand.add("^textedit js,#");
			knownFileFormatsCommand.add("^textedit null,#");
			knownFileFormatsCommand.add("^textedit fcis,#");

			knownFileFormatsCommand.add("^imgv #");
			knownFileFormatsCommand
					.add("^imgv #,GIFs only support 256 colors.");

			knownFileFormatsCommand.add("^imgv #");
			knownFileFormatsCommand
					.add("^imgv #,this jpeg format is 'jpeg' please change to jpg");
			knownFileFormatsCommand
					.add("^imgv #,this jpeg format is 'jpe' please change to jpg");
			knownFileFormatsCommand
					.add("^imgv #,this jpeg format is 'jif' please change to jpg");
			knownFileFormatsCommand
					.add("^imgv #,this jpeg format is 'jfif' please change to jpg");
			knownFileFormatsCommand
					.add("^imgv #,this jpeg format is 'jfi' please change to jpg");
			lastLocation = ".";

			knownFileFormatsCommand.add("^entedit #");
			knownFileFormatsCommand.add("^sprtedit #");
			write();
		}
		i_none = Integer.parseInt("000000", 16);
		i_digit = Integer.parseInt("114f00", 16);
		i_non = Integer.parseInt("9e9600", 16);
		i_string = Integer.parseInt("d90502", 16);
		i_state = Integer.parseInt("0c00e8", 16);
		i_comment = Integer.parseInt("f000cc", 16);
	}

	public static void write() {
		System.out.println("Rewriting preferences...");
		File preferences = new File("debvi.pref");
		try {
			FileOutputStream fos = new FileOutputStream(preferences);
			Properties prop = new Properties();
			String kff = "";
			String kffc = "";
			for (int i = 0; i < knownFileFormats.size(); i++) {
				kff += knownFileFormats.get(i) + "$";
			}
			for (int i = 0; i < knownFileFormatsCommand.size(); i++) {
				kffc += knownFileFormatsCommand.get(i) + "$";
			}
			prop.setProperty("tec_none", r_none);
			prop.setProperty("tec_digits", r_digit);
			prop.setProperty("tec_nons", r_non);
			prop.setProperty("tec_strings", r_string);
			prop.setProperty("tec_states", r_state);
			prop.setProperty("tec_comments", r_comment);
			prop.setProperty("kff", kff);
			prop.setProperty("kffc", kffc);
			prop.setProperty("lastLocation", lastLocation);
			prop.setProperty("lookAndFeel", theme);
			prop.store(fos, "Preferences for Debvi " + Debvi.getDebviVersion());
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
